// pages/open/open.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showShareMenu({
      withShareTicket: true,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return{
      title:'这是我开发的小程序',
      path:'/page/open',
      success(res){
        console.log('转发成功',res)
      },
      fail(res){
        console.log('转发失败')
      }
    }
  },

  //登录
  login:function(){
    wx.login({
      success: function(res) {
        wx.request({
          url: 'https://api.weixin.qq.com/sns/jscode2session',
          data: {
            'appid':'wx7098b36cfc030202',
            'secret':'',
            'js_code':res.code,
            'grant_type':'authorization_code'
          },
          header: {},
          method: 'GET',
          dataType: 'json',
          responseType: 'text',
          success: function(res) {
            console.log('获取用户信息成功',res)


            wx.getUserInfo({
              withCredentials: true,
              lang: 'zh_CN',
              success: function(res) {
                console.log('用户详细信息获取成功',res)
              },
              fail: function(res) {},
              complete: function(res) {},
            })
          },
          fail: function(res) {
            console.log('获取用户信息失败', res)
          },
          complete: function(res) {},
        })
        console.log(res)
      },
      fail: function(res) {

      },
      complete: function(res) {

      },
    })
  },

  // 检查登录
  checkSession:function(){
    wx.checkSession({
      success: function(res) {

      },
      fail: function(res) {

      },
      complete: function(res) {
        console.log('检查登录',res)
      },
    })
  },

  // 用户是否授权
  authorize:function(){
    wx.authorize({
      scope: 'scope.userInfo',
      success: function(res) {
        console.log('是否授权获取用户信息',res)
      },
      fail: function(res) {},
      complete: function(res) {},
    })

    wx.authorize({
      scope: 'scope.userLocation',
      success: function (res) {
        console.log('是否授权获取用户位置信息', res)
      },
      fail: function (res) { },
      complete: function (res) { },
    })

    wx.authorize({
      scope: 'scope.record',
      success: function (res) {
        console.log('是否授权使用录音功能', res)
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },


  // 获取分享状态
  getShareInfo:function(){
    wx.getShareInfo({
      shareTicket: 'true',
      success: function(res) {
        console.log('分享状态',res)
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  //选择地址
  chooseAddress: function () {
    wx.chooseAddress({
      success: function (res) {
        console.log('获取收货地址成功', res)
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  // 添加卡券
  addCard1:function(){
    wx.addCard({
      cardList: [{
        cardId:'1',
        cardExt: '{"code": "", "openid": "", "timestamp": "", "signature":""}'
      }],
      success: function(res) {
        console.log('添加成功',res)
      },
      fail: function(res) {
        console.log('添加失败', res)
      },
      complete: function (res) { console.log('完成', res)},
    })
  },

//打开微信设置
  openSetting:function(){
    wx.openSetting({
      success: function(res) {
        console.log('设置成功',res)
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  // 获取设置信息
  getSetting:function(){
    wx.getSetting({
      success: function(res) {
        console.log('获取配置成功',res)
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  // 获取微信运动信息
  getWeRunData:function(){
    wx.getWeRunData({
      success: function(res) {
        console.log('获取微信运动信息',res)
        
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },


  //获取发票抬头
  chooseInvoiceTitle:function(){
    wx.chooseInvoiceTitle({
      success: function(res) {
        console.log('获取发片信息成功',res)
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },


  //获取生物认证方式
  checkIsSupportSoterAuthentication:function(){
    wx.checkIsSupportSoterAuthentication({
      success: function(res) {
        wx.showToast({
          title: res.supportMode.join(''),
        })
        console.log('获取生物认证',res)
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },


  // 自定义数据上报
  reportAnalytics:function(){
    wx.reportAnalytics('eventname', {
      price:120,
      color:'red'
    })
  },

  // 检查更新
  onCheckForUpdate:function(){
    const updateManager = wx.getUpdateManager()
    updateManager.onCheckForUpdate(function(res){
      console.log(res)
    })

    updateManager.onUpdateReady(function(){
      console.log('下载成功')
    })

    updateManager.onUpdateFailed(function(){
      console.log('下载失败')
    })
  },

  // base64转换
  base64ToArrayBuffer:function(){
    const arrayBuffer = new Uint8Array([12,22,33])
    const base64 = wx.arrayBufferToBase64(arrayBuffer)
    console.log(base64)

    const base64Str = 'CxYh'
    const arrayBuffer1 = wx.base64ToArrayBuffer(base64Str)
    console.log(arrayBuffer1)
  },


  //多线程开发
  workers:function(){
    const worker = wx.createWorker('workers/request/index.js')

    worker.postMessage({
      msg: 'hello from worker',
      buffer: 12
    })

  
  }
})